import i18n from '@/i18n'
import { MetaTemplateDao } from '@apps/meta/domain/meta-template/meta-template.dao'
import { DomainModel } from '@apps/meta/domain/model'
import { Assist } from '@libs/lhy-common'
import { ApiResponsePage } from '@libs/lhy-core'
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { MetaTemplateBatchSaveDto } from './dto/meta-template-batch-save.dto'
import { MetaTemplateListGroupDto } from './dto/meta-template-list-group.dto'
import { MetaTemplatePageDto } from './dto/meta-template-page.dto'
import { MetaTemplateSaveDto } from './dto/meta-template-save.dto'
import { MetaTemplateTreeDto } from './dto/meta-template-tree.dto'
import { MetaTemplateUpdateDto } from './dto/meta-template-update.dto'
import { MetaTemplateService } from './meta-template.service'

@ApiTags(i18n.t('app-meta.meta-template.meta-template.12150114-0'))
@Controller({
  path: 'template',
  version: '1',
})
export class MetaTemplateController {
  constructor(
    private readonly templateDao: MetaTemplateDao,
    private readonly templateService: MetaTemplateService
  ) {}

  @ApiOperation({ summary: i18n.t('app-meta.meta-template.meta-template.12150114-1') })
  @Get('page')
  @ApiResponsePage(DomainModel.MetaTemplate)
  async page(@Query() param: MetaTemplatePageDto) {
    return this.templateDao.page(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-template.meta-template.12150114-2') })
  @Get('list_group')
  async listGroup(@Query() param: MetaTemplateListGroupDto) {
    return this.templateDao.listGroup(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-template.meta-template.12150114-3') })
  @Get('tree')
  async tree(@Query() param: MetaTemplateTreeDto) {
    return this.templateService.tree(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-template.meta-template.12150114-4') })
  @Put('save')
  async save(@Body() param: MetaTemplateSaveDto) {
    Assist.threw(
      await this.templateDao.isLabelExists([
        {
          label: param.label,
          groupLabel: param.groupLabel,
          platform: param.platform,
        },
      ]),
      i18n.t('app-meta.meta-template.meta-template.12150114-5', { args: [param.label] })
    )

    await this.templateDao.save({
      ...param,
      belong: DomainModel.MetaTemplateBelong.Platform,
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-template.meta-template.12150114-6') })
  @Put('batch_save')
  async batchSave(@Body() param: MetaTemplateBatchSaveDto) {
    if (param.cover) {
      await this.templateDao.batchUpsertOnUnique(param.templates, DomainModel.MetaTemplateBelong.Platform)
    } else {
      Assist.threw(
        await this.templateDao.isLabelExists(
          param.templates.map(template => ({
            platform: template.platform,
            groupLabel: template.groupLabel,
            label: template.label,
          }))
        ),
        i18n.t('app-meta.meta-template.meta-template.12150114-7')
      )

      await this.templateDao.batchSave(
        param.templates.map(template => ({
          ...template,
          belong: DomainModel.MetaTemplateBelong.Platform,
        }))
      )
    }
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-template.meta-template.12150114-8') })
  @Post('update')
  async update(@Body() param: MetaTemplateUpdateDto) {
    const oldData = Assist.notNull(
      await this.templateDao.getById(param.id),
      i18n.t('app-meta.meta-template.meta-template.12150114-9', { args: [param.id] })
    )
    Assist.threw(
      oldData.belong === DomainModel.MetaTemplateBelong.System,
      i18n.t('app-meta.meta-template.meta-template.12150114-10')
    )
    Assist.threw(
      await this.templateDao.isLabelExists([{ ...param, platform: oldData.platform }]),
      i18n.t('app-meta.meta-template.meta-template.12150114-5', { args: [param.label] })
    )

    await this.templateDao.updateById(param.id, param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-template.meta-template.12150114-11') })
  @Delete('del')
  async del(@Query('id') id: number) {
    const oldData = Assist.notNull(
      await this.templateDao.getById(id),
      i18n.t('app-meta.meta-template.meta-template.12150114-12', { args: [id] })
    )
    Assist.threw(
      oldData.belong === DomainModel.MetaTemplateBelong.System,
      i18n.t('app-meta.meta-template.meta-template.12150114-13')
    )

    await this.templateDao.deleteById(id)
  }
}
